package com.example.demo.service;

import org.apache.catalina.connector.Connector;
import org.apache.catalina.valves.AccessLogValve;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

import java.io.File;

@Component
public class CustomerService implements WebServerFactoryCustomizer<TomcatServletWebServerFactory> {
    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.setPort(8888);//tomcat端口
        factory.setBaseDirectory(new File("/home/caoyu/tmp"));//缓存路径
        factory.addContextValves(getLogAccessLogValue());//设置日志
        factory.addConnectorCustomizers(new MyTomcatConnectorCustomizer());//tomcat自定义访问连接
    }

    private AccessLogValve getLogAccessLogValue(){
        AccessLogValve log=new AccessLogValve();
        log.setDirectory("/home/caoyu/logs");//日志路径
        log.setEnabled(true);//启用日志
        log.setPattern("common");//输入日志格式
        log.setPrefix("springboot-access-log");//日志名称
        log.setSuffix(".log");//日志后缀
        return log;
    }
}

class MyTomcatConnectorCustomizer implements TomcatConnectorCustomizer {

    @Override
    public void customize(Connector connector) {
        Http11NioProtocol protocal=(Http11NioProtocol)connector.getProtocolHandler();
        protocal.setMaxConnections(20000);//最大连接数
        protocal.setMaxThreads(500);//最大线程数
    }

}